Skip to content

[LLD][COFF] Fix out-of-bounds write when filling gaps with INT3 in code sections - #180411

Merged
aganea merged 1 commit into
llvm:mainfrom
wbenny:main
Feb 10, 2026
Merged

[LLD][COFF] Fix out-of-bounds write when filling gaps with INT3 in code sections#180411
aganea merged 1 commit into
llvm:mainfrom
wbenny:main

Conversation

@wbenny

@wbenny wbenny commented Feb 8, 2026

Copy link
Copy Markdown
Contributor

When merging .bss into a code section (e.g., /MERGE:.bss=.text), the INT3 gap-filling loop in writeSections() would write past the output buffer. This happens because .bss chunks have hasData=false, so they contribute to VirtualSize but not SizeOfRawData. The loop was using chunk RVAs without checking if they exceeded the raw data region.

This caused a crash on Windows with /FILEALIGN:1 (access violation 0xC0000005). The tight alignment leaves no slack in the mapped buffer, so the overflow immediately hits unmapped memory.

The fix bounds all memset operations to rawSize and exits early when encountering chunks beyond the raw data boundary.

Fixes #180406

@github-actions

github-actions Bot commented Feb 8, 2026

Copy link
Copy Markdown

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot

llvmbot commented Feb 8, 2026

Copy link
Copy Markdown
Member

@llvm/pr-subscribers-lld-coff

@llvm/pr-subscribers-lld

Author: Petr Beneš (wbenny)

Changes

When merging .bss into a code section (e.g., /MERGE:.bss=.text), the INT3 gap-filling loop in writeSections() would write past the output buffer. This happens because .bss chunks have hasData=false, so they contribute to VirtualSize but not SizeOfRawData. The loop was using chunk RVAs without checking if they exceeded the raw data region.

This caused a crash on Windows with /FILEALIGN:1 (access violation 0xC0000005). The tight alignment leaves no slack in the mapped buffer, so the overflow immediately hits unmapped memory.

The fix bounds all memset operations to rawSize and exits early when encountering chunks beyond the raw data boundary.

Fixes #180406


Full diff: https://github.com/llvm/llvm-project/pull/180411.diff

1 Files Affected:

  • (modified) lld/COFF/Writer.cpp (+7-2)
diff --git a/lld/COFF/Writer.cpp b/lld/COFF/Writer.cpp
index 559bd387fa9cb..a5e30e26b9e5b 100644
--- a/lld/COFF/Writer.cpp
+++ b/lld/COFF/Writer.cpp
@@ -2619,12 +2619,17 @@ void Writer::writeSections() {
     if ((sec->header.Characteristics & IMAGE_SCN_CNT_CODE) &&
         (ctx.config.machine == AMD64 || ctx.config.machine == I386)) {
       uint32_t prevEnd = 0;
+      uint32_t rawSize = sec->getRawSize();
       for (Chunk *c : sec->chunks) {
         uint32_t off = c->getRVA() - sec->getRVA();
+        // Chunks without data (e.g., .bss) have virtual addresses beyond
+        // rawSize; stop filling when we reach the end of raw data.
+        if (off >= rawSize)
+          break;
         memset(secBuf + prevEnd, 0xCC, off - prevEnd);
-        prevEnd = off + c->getSize();
+        prevEnd = std::min(off + static_cast<uint32_t>(c->getSize()), rawSize);
       }
-      memset(secBuf + prevEnd, 0xCC, sec->getRawSize() - prevEnd);
+      memset(secBuf + prevEnd, 0xCC, rawSize - prevEnd);
     }
 
     parallelForEach(sec->chunks, [&](Chunk *c) {

@aganea

aganea commented Feb 8, 2026

Copy link
Copy Markdown
Member

This fix looks good, thanks. Are you able to craft a testcase please?

@wbenny

wbenny commented Feb 8, 2026

Copy link
Copy Markdown
Contributor Author

Amended the commit with a test case and verified it doesn't pass without the fix and passes with it.

If I understand correctly, the # REQUIRES: x86 applies to AMD64 too, and it should be there, because the CCs are guarded with if .. (ctx.config.machine == AMD64 || ctx.config.machine == I386)... right?

@aganea aganea left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, just a small nit.

# RUN: llvm-readobj --sections %t.exe | FileCheck %s

# CHECK: Name: .text
# CHECK-NEXT: VirtualSize: 0x

@aganea aganea Feb 9, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I guess we can write the virtual size that we're expecting here: 0x104

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

@wbenny
wbenny requested a review from aganea February 9, 2026 15:59
Comment thread lld/COFF/Writer.cpp
uint32_t off = c->getRVA() - sec->getRVA();
// Chunks without data (e.g., .bss) have virtual addresses beyond
// rawSize; stop filling when we reach the end of raw data.
if (off >= rawSize)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am wondering if this check could prevent from filling the gap between the last .text chunk and the .bss, if the file alignement is larger than the last .text chunk?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, the memset after the loop handles that.

@maj113 maj113 May 16, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this exposed a new bug I guess:

clang_release_ble/DM40Native.exe //20.1.8
  FileSize=55550 EP=0xd8b5 SizeOfImage=0xd8fe SizeOfHeaders=0x198
  [.data] VA=0x198 VSize=0xd766 RawOff=0x198 RawSz=0xd766 Chars=0xe0000020  VSize-RawSz=0

clang_release_ble/DM40Native_broken.exe //trunk
  FileSize=55710 EP=0xd949 SizeOfImage=0xd9a4 SizeOfHeaders=0x198
  [.data] VA=0x198 VSize=0xd80c RawOff=0x198 RawSz=0xd806 Chars=0xe0000020  VSize-RawSz=6

clang_release_bt/DM40Native.exe //20.1.8
  FileSize=52542 EP=0xccc2 SizeOfImage=0xcd3e SizeOfHeaders=0x198
  [.data] VA=0x198 VSize=0xcba6 RawOff=0x198 RawSz=0xcba6 Chars=0xe0000020  VSize-RawSz=0

clang_release_bt/DM40Native_broken.exe //trunk
  FileSize=52718 EP=0xcd6a SizeOfImage=0xcdf4 SizeOfHeaders=0x198
  [.data] VA=0x198 VSize=0xcc5c RawOff=0x198 RawSz=0xcc56 Chars=0xe0000020  VSize-RawSz=6

PE loader rejects the exe

seems to be due to:

if (c->hasData)
    rawSize = alignTo(virtualSize, config->fileAlign);

since bss is not handled in this block it gets excluded by this, locally for me this fixed it but this probably isn't the correct solution since filealign 2 and 4 would still probably be outside the rawSize:

if (config->fileAlign == 1 && rawSize < virtualSize)
    rawSize = virtualSize;

so maybe something like this would be better:

if (rawSize < alignTo(virtualSize, config->fileAlign))
    rawSize = alignTo(virtualSize, config->fileAlign);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure that this issue that you're observing is related to this commit? As far as I can see, this change shouldn't affect the final layout of the PE image (which can cause the image to be rejected), only what is filled in it.

Can you file a separate issue about the problem you're seeing, so we can try to track it down? Ideally, can you provider a reproducer? If you add -reproduce:repro.tar to the link command, it'll produce a file repro.tar which contains everything required for rerunning the link command. That would allow others to pinpoint exactly which LLD change is causing the PE loader to reject it. (If linking through the compiler driver in mingw mode, pass -Wl,--reproduce=repro.tar instead.)

@aganea
aganea enabled auto-merge (squash) February 10, 2026 13:17
@aganea
aganea merged commit 6558595 into llvm:main Feb 10, 2026
10 checks passed
@github-actions

Copy link
Copy Markdown

@wbenny Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

@Andarwinux

Copy link
Copy Markdown
Member

#198126

Would be nice yeah, the VS bundled clang/lld barely gets updated I'd rather not wait a year for this...

Can we backport this to release/22?

@mstorsjo mstorsjo added this to the LLVM 22.x Release milestone May 17, 2026
@github-project-automation github-project-automation Bot moved this to Needs Triage in LLVM Release Status May 17, 2026
@github-project-automation github-project-automation Bot moved this from Needs Triage to Done in LLVM Release Status May 17, 2026
@mstorsjo

Copy link
Copy Markdown
Member

/cherry-pick 6558595

@mstorsjo

Copy link
Copy Markdown
Member

Would be nice yeah, the VS bundled clang/lld barely gets updated I'd rather not wait a year for this...

Can we backport this to release/22?

We can try; we're already kinda far in the 22 release series, so the release managers are getting more selective in what fixes they approve for backports, but we can see what they say about this.

@llvmbot

llvmbot commented May 17, 2026

Copy link
Copy Markdown
Member

/pull-request #198212

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Development

Successfully merging this pull request may close these issues.

lld-link crashes with /FILEALIGN:1 since 21.1.8 (only on Windows)

6 participants